// function qs(ele) {
//     return document.querySelector(ele)
// }

// 接口地址：https://www.apifox.cn/apidoc/shared-36f0cbc0-9064-44bc-850a-805e263bf494/api-25827426
// 简化箭头函数
axios.defaults.baseURL = 'http://124.223.14.236:3001/api'

const qs = element => document.querySelector(element)


// 封装优化acync,await
// 封装省
async function getProvinceData() {
    const { data: res } = await axios.get('/city/province')
    console.log(res.data);  //省份数组

    //map join 渲染
    const provinceStr = res.data.map(
        (item) => `<option value="${item.province}">${item.name}</option>`
    ).join('')
    // console.log(provinceStr)
    qs('#province').innerHTML = '<option value="">请选择省份</option>' + provinceStr
}
// 封装市
async function getCityData(provinceCode) {
    const {
        data: { data: cityData }, } = await axios.get('/city/city', {
            params: {
                provinceCode,
            }
        })
    // console.log(cityData);

    const cityStr = cityData.map(item => `<option value="${item.city}">${item.name}</option>`).join('')

    qs('#city').innerHTML = `<option value="">请选择城市</option>` + cityStr

}
// 封装区
async function getAreaData(provinceCode, cityCode) {
    const {
        data: { data: townData }, } = await axios.get('/city/area', {
            params: {
                provinceCode,
                cityCode
            }
        })
    // console.log(townData);

    const townStr = townData.map(item => `<option value="${item.area}">${item.name}</option>`).join('')

    qs('#county').innerHTML = `<option value="">请选择区</option>` + townStr
}


async function init() {

    await getProvinceData()
    qs('#province').value = 34

    await getCityData('34')
    qs('#city').value = '01'

    await getAreaData('34', '01')
    qs('#county').value = '02'

}
init()

qs('#province').addEventListener('change', async function () {
    await getCityData(this.value)
    qs('#county').innerHTML = `<option value="">请选择区</option>`
})

qs('#city').addEventListener('change', async function () {
    await getAreaData(qs('#province').value,this.value)
})